package com.yxjx.duoxue.course;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.PageLoading;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.datamodel.DMCourseCategory;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.utils.UIHelper;

public class DetailedCourseTypesActivity extends BaseActionBarActivity {

	private static final int MSG_CODE_LOAD_TYPES_DONE = 1;
	private ListView level1ListView;
	private GridView level2GridView;
	private Level1Adapter level1Adapter;
	private Level2Adapter level2Adapter;
	protected ArrayList<DMCourseCategory> courseTypes;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setCustomActionBar(R.layout.head_course_search_2);
		setContentView(R.layout.fragment_detailed_course_types);

		UIHelper.setClickListener(mActionBar.getCustomView(), R.id.search_root, mClickListener);
		showLoading(true);

		level1ListView = (ListView) findViewById(R.id.level1);
		level2GridView = (GridView) findViewById(R.id.level2);
		level1Adapter = new Level1Adapter();
		level2Adapter = new Level2Adapter();
		level1ListView.setAdapter(level1Adapter);
		level2GridView.setAdapter(level2Adapter);

		level1ListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				level1ListView.setSelection(arg2);
				setLevel1Index(arg2);
				for (int i = 0; i < arg0.getChildCount(); i++) {
					if (i != arg2) {
						arg0.getChildAt(i).findViewById(R.id.root).setSelected(false);
					}
				}
				arg0.getChildAt(arg2).findViewById(R.id.root).setSelected(true);
			}
		});

		level2GridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				DMCourseCategory item= level2Adapter.getData().get(arg2);
				Intent intent = new Intent(DetailedCourseTypesActivity.this, CourseListActivity.class);
				intent.putExtra(HomePageFragment.EXTRA_SEARCH_CATEGORY_ID, item.getType());
				intent.putExtra(HomePageFragment.EXTRA_SEARCH_CATEGORY, item.getStr());
				startActivity(intent);
			}
		});

		new Thread(new Runnable() {

			@Override
			public void run() {
				PageLoading.getCourseCategory(DetailedCourseTypesActivity.this);
				mHandler.sendEmptyMessage(MSG_CODE_LOAD_TYPES_DONE);
			}
		}).start();
	}

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_CODE_LOAD_TYPES_DONE:
				showLoading(false);
				courseTypes = Globals.getInstance(DetailedCourseTypesActivity.this)
						.getDetailedCourseTypes();
				level1Adapter.setData(courseTypes);
				level1ListView.post(new Runnable() {
					
					@Override
					public void run() {
						if (level1Adapter.getCount() <= 0) {
							return;
						}
						setLevel1Index(0);
						level1ListView.getChildAt(0).findViewById(R.id.root).setSelected(true);
					}
				});
				break;
			default:
				break;
			}
		}
	};
	private int level1Index = -1;

	private void setLevel1Index(int index) {
		if (level1Index == index) {
			return;
		}
		level1Index  = index;
		if (courseTypes == null || courseTypes.size() == 0) {
			return;
		}
		if (index < 0 || index >= courseTypes.size()) {
			return;
		}
		level2Adapter.setData(courseTypes.get(index).getSubCategories());
	}

	private void showLoading(boolean showLoading) {
		findViewById(R.id.root).setVisibility(
				showLoading ? View.INVISIBLE : View.VISIBLE);
		findViewById(R.id.loading).setVisibility(
				showLoading ? View.VISIBLE : View.INVISIBLE);
	}

	private void showLevel2Empty(boolean showLevel2Empty) {
		findViewById(R.id.level2).setVisibility(
				showLevel2Empty ? View.INVISIBLE : View.VISIBLE);
		findViewById(R.id.empty).setVisibility(
				showLevel2Empty ? View.VISIBLE : View.INVISIBLE);
	}

	class Level1Adapter extends BaseAdapter {

		private ArrayList<DMCourseCategory> mData;

		@Override
		public int getCount() {
			return mData == null ? 0 : mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret = null;
			if (arg1 == null) {
				ret = LayoutInflater.from(DetailedCourseTypesActivity.this)
						.inflate(R.layout.item_course_types_level_one, null);
			} else {
				ret = arg1;
			}
			DMCourseCategory item = mData.get(arg0);
			UIHelper.setText(ret, R.id.text, item.getStr());
			UIHelper.asyncLoadImage((ImageView) ret.findViewById(R.id.icon),
					item.getIconUrl(), R.drawable.btn_home_default);
			return ret;
		}

		public void setData(ArrayList<DMCourseCategory> mData) {
			this.mData = mData;
			notifyDataSetChanged();
		}

	}

	class Level2Adapter extends BaseAdapter {

		private ArrayList<DMCourseCategory> mData;

		@Override
		public int getCount() {
			return mData == null ? 0 : mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret = null;
			if (arg1 == null) {
				ret = LayoutInflater.from(DetailedCourseTypesActivity.this)
						.inflate(R.layout.item_course_type_level_two, null);
			} else {
				ret = arg1;
			}
			DMCourseCategory item = mData.get(arg0);
			UIHelper.setText(ret, R.id.text, item.getStr());
			UIHelper.asyncLoadImage((ImageView) ret.findViewById(R.id.icon),
					item.getIconUrl(), R.drawable.default_icon);
			return ret; 
		}
		
		public ArrayList<DMCourseCategory> getData() {
			return this.mData;
		}

		public void setData(ArrayList<DMCourseCategory> data) {
			this.mData = data;
			boolean showLevel2Empty = (mData == null || mData.size() == 0);
			showLevel2Empty(showLevel2Empty);
			notifyDataSetChanged();
			level2GridView.post(new Runnable() {

				@Override
				public void run() {
					level2GridView.setSelection(0);
				}
			});
		}
	}

	private View.OnClickListener mClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.search_root:
				startActivity(new Intent(DetailedCourseTypesActivity.this, CourseSearchActivity.class));
				break;
			default:
				mBaseClickListener.onClick(arg0);
				break;
			}

		}
	};

}
